﻿Fingerbrisk.components = {

    type: {
        
        document: 0,

        script: {

            inline: 1,
            prototype: 2
        },
        css: {

            inline: 3,
            prototype: 4
        },
        page: {
            title: 5,
            head: 6,
            body: 7
        }
    },

    addComponent: function (type, msg) {

        switch (type) {
        
            case this.type.document:
                Fingerbrisk.components.addDocument(msg.data, msg.callback, msg.context);
                break;
            case this.type.script.inline:
                eval(msg.data);

                if (typeof msg.callback == "function")
                    msg.callback.call(msg.context);
                break;
                
            case this.type.script.prototype:
                Fingerbrisk.components.addExternalScript(msg.data, msg.callback, msg.context);
                break;

            case this.type.css.prototype:
                Fingerbrisk.components.addExternalCss(msg.data, msg.callback, msg.context);
                break;
        }
    }
};

Fingerbrisk.components.addExternalScript = function (path, callback, context) {

    var script = document.createElement("SCRIPT");
    script["src"] = path;
    script["type"] = "text/javascript";
    script["language"] = "javascript";

    script.onload = function () {

        if (typeof callback == "function")
            callback.call(context, { "status": "success" });
    };

    document.getElementsByTagName("head")[0].appendChild(script);
};

Fingerbrisk.components.addExternalCss = function (path, callback, context) {

    var css = document.createElement("LINK");
    css["href"] = path;
    css["type"] = "text/css";
    css["rel"] = "stylesheet";

    css.onload = function () {

        if (typeof callback == "function")
            callback.call(context, { "status": "success" });
    };

    document.getElementsByTagName("head")[0].appendChild(css);
};

Fingerbrisk.components.addDocument = function (params, callback, context) {

    params.successCallback = callback;
    params.context = context;

    (new Fingerbrisk.docRequest.Request(params)).send();
};